package com.example.zhu.controller.employee;

import com.example.zhu.Mapper.EmployeeMapper;
import com.example.zhu.Mapper.OrderMapper;
import com.example.zhu.Mapper.RoomMapper;
import com.example.zhu.VO.CommodityVO.CommodityVO;
import com.example.zhu.VO.EmployeeVO.EmployeeVO;
import com.example.zhu.VO.RoomVo.RoomVO;
import com.example.zhu.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/employee")
@Slf4j
public class employeeController {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RoomMapper roomMapper;

    @PostMapping("/page")
    public Reply page(@RequestBody Employee employee){
        String name = employee.getName();
        String phone = employee.getPhone();
        MyPage page = employee.getPage();
        page.setMin((page.getPage()-1)*page.getSize());
        page.setMax((page.getPage())*page.getSize());
        List<Employee>EmployeeAllList;
        if(name!=null){
            name="%"+name.trim()+"%";
            EmployeeAllList = employeeMapper.getNameList(name);
        }else if(phone!=null){
            phone="%"+phone.trim()+"%";
            EmployeeAllList = employeeMapper.getPhoneList(phone);
        }else{
            EmployeeAllList = employeeMapper.getAllList(page);
        }
        EmployeeVO employeeVO = new EmployeeVO();
        employeeVO.setRecords(EmployeeAllList);
        employeeVO.setSize(page.getSize()+"");
        employeeVO.setCurrent(page.getPage()+"");
        Integer allListCount = Integer.valueOf(employeeMapper.getAllListCount());
        employeeVO.setTotal(allListCount);
        employeeVO.setSearchCount(true);
        employeeVO.setPages(String.valueOf(allListCount/page.getSize()+1));
        return Reply.success(employeeVO);

    }
    @PostMapping("/modify")
    public Reply<Employee>  modify(@RequestBody Employee employee){
        Employee oldEmployee = employeeMapper.selectById(employee);
        if (oldEmployee!=null){
            employee.setUpdateTime(new Date());
            employeeMapper.updataById(employee);
            return Reply.success(employee);
        }else{
            // "修改失败"
            return Reply.failed(employee);
        }
    }
    @GetMapping("/delete/{id}")
    public Reply<Employee>  delete(@PathVariable(name = "id") String id){
        Employee employee = new Employee();
        employee.setId(id);
        Employee oldEmployee = employeeMapper.selectById(employee);
        if (oldEmployee!=null){
            employeeMapper.Delect(oldEmployee);
            return Reply.success(employee);
        }else{
            // "修改失败"
            return Reply.failed(employee);
        }
    }
    @PostMapping("/distribution")
    public Reply<Employee>  add(@RequestBody Employee employee){
        employee.setUpdateTime(new Date());
        employeeMapper.Insert(employee);
          return Reply.success(employee);

    }
    @PostMapping("/all")
    public Reply<List<Employee>>  all(){
        List<Employee> commoditys=  employeeMapper.selectByStatus(1);
        List<Employee> updatedCommodities = commoditys.stream().peek(commodity -> commodity.setCount(0))
                .collect(Collectors.toList());
        return Reply.success(updatedCommodities);

    }
    @PostMapping("/OrderAll")
    public Reply<List<Employee>>  OrderAll(@RequestBody RoomVO roomVO ){
        RoomVO room =  roomMapper.selectName(roomVO);
        String orderId = room.getOrderId();
        Order order= orderMapper.selectByid(orderId);
        String nicknames = order.getNickname();
        String[] nicknameArray = nicknames.split(" ");
        List<Employee> employees=new ArrayList<>();
        for (String nickname : nicknameArray) {
           Employee employee= employeeMapper.selectByNickName(nickname);
           employees.add(employee);
        }
        List<Employee> employees1 = employeeMapper.selectByStatus(1);
        employees.addAll(employees1);
        return Reply.success(employees);
    }
    @PostMapping("/OrderId")
    public Reply<List<Employee>>  OrderId(@RequestBody RoomVO roomVO ){
        String orderId = roomVO.getOrderId();
        Order order= orderMapper.selectByid(orderId);
        String nicknames = order.getNickname();
        String[] nicknameArray = nicknames.split(" ");
        List<Employee> employees=new ArrayList<>();
        for (String nickname : nicknameArray) {
            Employee employee= employeeMapper.selectByNickName(nickname);
            employees.add(employee);
        }
        List<Employee> employees1 = employeeMapper.selectByStatus(1);
        employees.addAll(employees1);
        return Reply.success(employees);
    }
    @PostMapping("/alls")
    public Reply<List<Employee>>  alls(){
        List<Employee> commoditys=  employeeMapper.selectAll();
        return Reply.success(commoditys);

    }
}
